package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.BusOutsourceVo;
import org.dromara.business.domain.bo.BusOutsourceBo;
import org.dromara.business.service.IBusOutsourceService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 资源
 *
 * @author Lion Li
 * @date 2024-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/outsource")
public class BusOutsourceController extends BaseController {

    private final IBusOutsourceService busOutsourceService;

    /**
     * 查询资源列表
     */
    @SaCheckPermission("business:outsource:list")
    @GetMapping("/list")
    public TableDataInfo<BusOutsourceVo> list(BusOutsourceBo bo, PageQuery pageQuery) {
        return busOutsourceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出资源列表
     */
    @SaCheckPermission("business:outsource:export")
    @Log(title = "资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusOutsourceBo bo, HttpServletResponse response) {
        List<BusOutsourceVo> list = busOutsourceService.queryList(bo);
        ExcelUtil.exportExcel(list, "资源", BusOutsourceVo.class, response);
    }

    /**
     * 同步资源
     */
    @SaCheckPermission("business:outsource:sync")
    @Log(title = "资源", businessType = BusinessType.SYNC)
    @RepeatSubmit()
    @PostMapping("/sync")
    public R<Void> sync() {
        return toAjax(busOutsourceService.sync());
    }

    /**
     * 获取资源详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:outsource:query")
    @GetMapping("/{id}")
    public R<BusOutsourceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busOutsourceService.queryById(id));
    }

    /**
     * 新增资源
     */
    @SaCheckPermission("business:outsource:add")
    @Log(title = "资源", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusOutsourceBo bo) {
        return toAjax(busOutsourceService.insertByBo(bo));
    }

    /**
     * 修改资源
     */
    @SaCheckPermission("business:outsource:edit")
    @Log(title = "资源", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusOutsourceBo bo) {
        return toAjax(busOutsourceService.updateByBo(bo));
    }

    /**
     * 删除资源
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:outsource:remove")
    @Log(title = "资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busOutsourceService.deleteWithValidByIds(List.of(ids), true));
    }
}
